package com.qzdatasoft;
//package com.xflib.framework.ratelimit.core;

import com.qzdatasoft.ratelimit.bean.RateLimitRule;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 分层限流器配置类
 */
public class HierarchicalRateLimiterConfig {

    // 存储限流规则的映射表,
    // key = 规则键，如
    //   ip:url
    //   userAccount:url
    //   ip|userAccount:url
    // value = 限流规则
    private final ConcurrentHashMap<String, RateLimitRule> rules = new ConcurrentHashMap<>();

    /**
     * 添加规则
     */
    public void addRule(String key, RateLimitRule rule) {
        rules.put(key, rule);
    }

    public RateLimitRule getRule(String key) {
        return rules.get(key);
    }

    /**
     * 删除规则
     */
    public void removeRule(String key) {
        rules.remove(key);
    }

    /**
     * 限流规则类
     */
//    public static class RateLimitRuleDto {
//        private final int ratePerSecond; // 每秒请求数
//        private final int burstCapacity; // 突发容量
//        private final boolean nodelay;   // 是否无延迟处理
//
//        public RateLimitRuleDto(int ratePerSecond, int burstCapacity) {
//            this(ratePerSecond, burstCapacity, false);
//        }
//
//        public RateLimitRuleDto(int ratePerSecond, int burstCapacity, boolean nodelay) {
//            this.ratePerSecond = ratePerSecond;
//            this.burstCapacity = burstCapacity;
//            this.nodelay = nodelay;
//        }
//
//        public int getRatePerSecond() {
//            return ratePerSecond;
//        }
//
//        public int getBurstCapacity() {
//            return burstCapacity;
//        }
//
//        public boolean isNodelay() {
//            return nodelay;
//        }
//    }

}